package com.zyh.contract.service.impl;

import com.zyh.common.utils.DateUtils;
import com.zyh.common.utils.StringUtils;
import com.zyh.contract.domain.RentalContract;
import com.zyh.contract.mapper.RentalContractMapper;
import com.zyh.contract.service.IRentalContractService;
import com.zyh.payment.domain.RentPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 租赁管理Service业务层处理
 * 
 * @author zyh
 * @date 2025-06-16
 */
@Service
public class RentalContractServiceImpl implements IRentalContractService 
{
    @Autowired
    private RentalContractMapper rentalContractMapper;

    /**
     * 查询租赁管理
     * 
     * @param id 租赁管理主键
     * @return 租赁管理
     */
    @Override
    public RentalContract selectRentalContractById(Long id)
    {
        return rentalContractMapper.selectRentalContractById(id);
    }

    /**
     * 查询租赁管理列表
     * 
     * @param rentalContract 租赁管理
     * @return 租赁管理
     */
    @Override
    public List<RentalContract> selectRentalContractList(RentalContract rentalContract)
    {
        return rentalContractMapper.selectRentalContractList(rentalContract);
    }

    /**
     * 新增租赁管理
     * 
     * @param rentalContract 租赁管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRentalContract(RentalContract rentalContract)
    {
        rentalContract.setCreateTime(DateUtils.getNowDate());
        int rows = rentalContractMapper.insertRentalContract(rentalContract);
        insertRentPayment(rentalContract);
        return rows;
    }

    /**
     * 修改租赁管理
     * 
     * @param rentalContract 租赁管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRentalContract(RentalContract rentalContract)
    {
        rentalContract.setUpdateTime(DateUtils.getNowDate());
        rentalContractMapper.deleteRentPaymentByContractId(rentalContract.getId());
        insertRentPayment(rentalContract);
        return rentalContractMapper.updateRentalContract(rentalContract);
    }

    /**
     * 批量删除租赁管理
     * 
     * @param ids 需要删除的租赁管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRentalContractByIds(Long[] ids)
    {
        rentalContractMapper.deleteRentPaymentByContractIds(ids);
        return rentalContractMapper.deleteRentalContractByIds(ids);
    }

    /**
     * 删除租赁管理信息
     * 
     * @param id 租赁管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRentalContractById(Long id)
    {
        rentalContractMapper.deleteRentPaymentByContractId(id);
        return rentalContractMapper.deleteRentalContractById(id);
    }

    /**
     * 新增支付管理信息
     * 
     * @param rentalContract 租赁管理对象
     */
    public void insertRentPayment(RentalContract rentalContract)
    {
        List<RentPayment> rentPaymentList = rentalContract.getRentPaymentList();
        Long id = rentalContract.getId();
        if (StringUtils.isNotNull(rentPaymentList))
        {
            List<RentPayment> list = new ArrayList<RentPayment>();
            for (RentPayment rentPayment : rentPaymentList)
            {
                rentPayment.setContractId(id);
                list.add(rentPayment);
            }
            if (list.size() > 0)
            {
                rentalContractMapper.batchRentPayment(list);
            }
        }
    }
}
